$(function() {
	
	var code = sessionStorage.getItem("code")
	var name = sessionStorage.getItem("name")
	var id_prikey = getRequest('id_prikey')
	
	if(id_prikey != 0) {
		$.post("selectPrizeByCode", { //根据类型获取奖品
			"code": code
		}, function(data) {

			if(data.code == 0) {
				var html = '<option value="">请选择奖品</option>';
				var rel = data.data
				for(var i in rel) {
					html += '<option value="' + rel[i].id_prikey + '">' + rel[i].奖品名称 + '</option>'
				}
				$("#jp").html(html)
				$.post("selectActivityById", {
					id_prikey: id_prikey
				}, function(data) {
					/*alert(JSON.stringify(data))*/
					var rel = data.data
					$("#name").val(rel.姓名)
					$("#sfzh").val(rel.身份证号)
					$("#num").val(rel.账号4)
					$("#jp").val(rel.奖品)

					code = rel.活动类型
					mui.init()
				})
			}

		})
	} else {
		$.post("selectPrizeByCode", { //根据类型获取奖品
			"code": code
		}, function(data) {

			if(data.code == 0) {
				var html = '<option value="">请选择奖品</option>';
				var rel = data.data
				for(var i in rel) {
					html += '<option value="' + rel[i].id_prikey + '">' + rel[i].奖品名称 + '</option>'
				}
				$("#jp").html(html)
				mui.init()
			}
		})
	}

	$("title").text(name)
	//根据类型获取奖品

	$("#tj").click(function() {
		var name = $("#name").val()
		var sfzh = $("#sfzh").val()
		if(name == "") {
			mui.alert("请输入客户姓名")
			return
		}

		if(sfzh == "") {
			mui.alert("请输入客户身份证号")
			return
		}
		var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)|(^\d{17}(\d|X|x)(\d{2})$)/;
		if(reg.test(sfzh) == false) {
			mui.alert("请输入正确的身份证号")
			return
		}
		sfzh = Base64.encode(sfzh)
		var prize = $("#jp").val()
		if(prize == "") {
			mui.alert("请选择优惠券")
			return
		}

		var num = $("#num").val()
		if(num == "") {
			mui.alert("请输入优惠券数量")
			return
		}

		var send = {
			"name": name,

			"prize": prize,
			"id_prikey": id_prikey,
			"activityType": code,
			"IDCard": sfzh,
			"number4": num,
		}

		mui.showLoading("正在加载..", "div")
		tjsj(send)

	})

	function tjsj(send) {
		$.post("insertActivityNew", send, function(data) {
			/*alert(JSON.stringify(data))*/
			if(data.code == 0) {
				mui.hideLoading();
				mui.alert("提交成功", function() {
					window.location.reload()
				})
			} else {
				mui.alert(data.msg)
				mui.hideLoading();
			}
		})

	}
})